package POJO;

import javax.servlet.http.HttpSession;
import DB.TripManager;
import DB.RelationManager;
import DB.DBInfo;
import java.util.Vector;

/**
 *
 * @author bram
 */
public class Layout {

    private HttpSession session;

    public Layout(HttpSession session) {
        this.session = session;
    }

    public HttpSession GetSession() {
        return session;
    }

    public String Header() {
        String output = "<div id=\"top\"><div class=\"inner\"><a href=\"index.jsp\"><img src=\"images/header.gif\" alt=\"Hello World\" /></a></div></div>";
        if (session.getAttribute("user") != null && ((User) session.getAttribute("user")).username != null) {
            output += "<div id=\"usermenu\">" +
                    "<a href=\"index.jsp?page=profile\">Profile</a>" +
                    "&nbsp;&nbsp;&nbsp;<a href=\"index.jsp?page=logout\">Log out</a></div>";
        } else {
            output += "<div id=\"usermenu\"><a href=\"index.jsp?page=register\">Register</a>" +
                    "&nbsp;&nbsp;&nbsp;<a href=\"index.jsp?page=login\">Log in</a></div>";
        }
        return output + "<div id=\"container\"><div id=\"content\"><div><table><tr><td><table class=\"centering\"><tr><td>";
    }

    public static boolean contains(Vector<Integer> v, int s) {
        for (int i = 0; i < v.size(); ++i) {
            if (v.elementAt(i) == s) {
                return true;
            }
        }
        return false;
    }

    public String Navigation() {
        String output = "<div id=\"menu\"><div><div>";
        User user = (User) session.getAttribute("user");
        if (user.username != null) {
            TripManager tm = new TripManager(DBInfo.getConnection());
            Vector<Integer> tripNames = new Vector();
            output += "<div class=\"subitem\"><a href=\"index.jsp\">Home</a></div>";
            output += "<div class=\"subitem\"><a href=\"index.jsp?page=profile\">Profile</a></div>";
            output += "<div class=\"item\">Own Trips:</div>";
            int ownedArray[] = tm.getTripsOwnedBy(user.username);
            if (ownedArray != null && ownedArray.length > 0) {
                for (int i = 0; i < ownedArray.length; i++) {
                    if (!contains(tripNames, ownedArray[i])) {
                        tripNames.add(ownedArray[i]);
                        output += "<div class=\"subitem\"><a href=\"index.jsp?page=trip&amp;tripid=" + ownedArray[i] +
                                "\">" + tm.getTripName(ownedArray[i]) + "</a></div>";
                    }
                }
            } else {
                output += "<div class=\"subitem\">No trips found.</div>";
            }
            output += "<div class=\"subitem\"><a href=\"index.jsp?page=addTrip\">Add a new trip</a></div>";

            output += "<div class=\"item\">Family Trips:</div>";
            RelationManager rm = new RelationManager(tm.conn);
            String familyArray[] = rm.getFamily(user.username);

            if (familyArray != null && familyArray.length > 0) {
                for (int i = 0; i < familyArray.length; i++) {
                    int familyOwned[] = tm.getTripsOwnedBy(familyArray[i]);
                    for (int j = 0; j < familyOwned.length; j++) {
                        if (!contains(tripNames, familyOwned[j])) {
                            tripNames.add(familyOwned[j]);
                            output += "<div class=\"subitem\"><a href=\"index.jsp?page=trip&amp;tripid=" + familyOwned[j] +
                                    "\">" + tm.getTripName(familyOwned[j]) + "</a></div>";
                        }
                    }
                }
            } else {
                output += "<div class=\"subitem\">No trips found</div>";
            }
            output += "<div class=\"item\">Friends Trips:</div>";
            String friendsArray[] = rm.getFriends(user.username);
            if (friendsArray != null && friendsArray.length > 0) {
                for (int i = 0; i < friendsArray.length; i++) {
                    int friendsOwned[] = tm.getTripsOwnedBy(friendsArray[i]);
                    for (int j = 0; j < friendsOwned.length; j++) {
                        if (!contains(tripNames, friendsOwned[j])) {
                            tripNames.add(friendsOwned[j]);
                            output += "<div class=\"subitem\"><a href=\"index.jsp?page=trip&amp;tripid=" + friendsOwned[j] +
                                    "\">" + tm.getTripName(friendsOwned[j]) + "</a></div>";
                        }
                    }
                }
            } else {
                output += "<div class=\"subitem\">No trips found</div>";
            }
        } else {
            output += "<div class=\"subitem\"><a href=\"index.jsp\">Home</a></div>";
            output += "<div class=\"item\">Not logged in:</div>" +
                    "<div class=\"subitem\"><a href=\"index.jsp?page=login\">Login</a></div>" +
                    "<div class=\"subitem\"><a href=\"index.jsp?page=register\">Register</a></div>";
        }
        output += "</div>";

        output += "<div><form method=\"post\" id=\"goToUser\" action=\"\"><div style=\"text-align:center;\"><input autocomplete=\"off\" onblur=\"fill();\" onclick=\"this.value='';\" size=\"20\" style=\"width:160px;\" id=\"inputString\" class=\"inputString\" onkeyup=\"lookup(this.value);\" type=\"text\" value=\"Search for user\"/><br /></div><div class=\"suggestionsBox\" id=\"suggestions\" style=\"display: none;\"><div class=\"suggestionList\" id=\"autoSuggestionsList\"> &nbsp;</div></div></form></div>";
        output += "<div style=\"text-align:center;\"><a href=\"feed.xml\"><img src=\"xml.gif\" width=\"36\" height=\"14\" alt=\"XML/RSS feed\" /><img src=\"rss.gif\" width=\"36\" height=\"14\" alt=\"XML/RSS feed\" /></a></div>";
        output += "</div></div>";

        return output;
    }

    public String Footer() {
        return "</td></tr></table><div id=\"footer\">All images, videos and other media are " +
                "property of their respective owners.<br />" +
                "HelloWorld inc. cannot be held responsible for this site's user-generated " +
                "content.<br />All else is ©2008 HelloWorld.</div></td></tr></table></div></div></div>";
    }

    public String Begin(String title) {
        return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
                "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
                "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" +
                "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
                "<title> Hello World - " + title + "</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /></head><body> <div id=\"main\">";
    }

    public String End() {
        return "</div></body></html>";
    }
}
